package shapewar.net.client;

import shapewar.a.widgets.Version;
import shapewar.engine.calc.Pos;
import shapewar.engine.ext.Model;
import shapewar.engine.intefac.ITank;
import shapewar.engine.model.MyTank;
import shapewar.engine.model.Tank;
import shapewar.engine.presenter.Presenter;
import shapewar.net.seriz.CreateInfo;
import shapewar.net.seriz.PosInfo;
import shapewar.net.seriz.Info;
import shapewar.net.seriz.NetConf;

public class User {
	private Tank tank;
	private Presenter pst;
	public CreateInfo info = new CreateInfo();
	
	public User(Presenter pst){
		this.pst = pst;
	}
	
	public void createTank(Presenter pst,boolean isPlayer,CreateInfo ci){
		if(info.state==NetConf.state_alive)return;
		
		if(isPlayer){
			tank = new MyTank(pst,ci.id,ci.x,ci.y,Model.getDefaultTankModel());
		}else{
			tank = new Tank(pst,ci.id,ci.x,ci.y,Model.getDefaultTankModel());
		}
//		if(pst.calcer.limitPos(new Pos(ci.x,ci.y), tank)){
//			requirePosReset();
//		}
		info = ci;
		info.state = NetConf.state_alive;
		tank.init();
		tank.setUser(this);
		tank.setTargetPos(new Pos(ci.target_x,ci.target_y));
		pst.ect.arranger.update();
	}
	
	public Tank getTank(){
		return tank;
	}
	
	public void moveTo(float x,float y,int bltKey){
		//bltKey: -2 for don't fire , others is bullet_key
		tank.moveTo(new Pos(x,y));
		if(bltKey!=-2)tank.fire(bltKey);
	}
	
	public void fire(int bltKey) {
		tank.fire(bltKey);
	}
	
	public void flash(){
		tank.flash();
	}
	
	public void protectorMinus(){
		tank.getProtector().minus();
	}
	
	public void protectorPlus(int n){
		tank.getProtector().plus(n);
	}
	
	public void die(Tank killer, int bulletKey,int ptrNum){
		tank.die(killer,bulletKey,ptrNum);
		pst.ect.arranger.update();
		info.state = NetConf.state_die;
	}
	
	public void relive(boolean isPlayer){
		createTank(pst, isPlayer, info);
	}
	
//	public void posReset(PosInfo pi) {
//		if(pst.calcer.limitPos(new Pos(pi.x,pi.y), tank)){
//			System.out.println("POS RESET 1");
//			requirePosReset();
//		}else{
//			System.out.println("POS RESET 0");
//			tank.setPos(new Pos(pi.x,pi.y));
//			tank.setTargetPos(new Pos(pi.x,pi.y));
//		}
//	}
	public void exit() {
		if(pst.ect.client!=null)
			pst.ect.client.room.exitById(tank.getId());
		tank.destory();
		pst.ect.arranger.update();
		info.state = NetConf.state_exit;
	}
	
	// ================== Require  ==============
	
	public void requireCreate(String name){
		pst.ect.client.sender.reqCreate(name);
	}
	public void requireMove(Pos pos, boolean fire) {
		pst.ect.client.sender.reqMove(tank, pos, fire?(int)(Version.timespeed*Math.random()*1000):-2);
	}
	public void requireFlash(Pos pos) {
		pst.ect.client.sender.reqFlash(tank,pos.x,pos.y);
	}
	public void requireDie(ITank killed,ITank killer,int bltKey,int ptrNum){
		pst.ect.client.sender.reqTankDie(killed.getInstance(), killer.getInstance(), bltKey, ptrNum);
	}
	public void requireProtectorMinus(ITank killed,ITank killer,int bltKey){
		pst.ect.client.sender.reqProtectorDie(killed.getInstance(), killer.getInstance(), bltKey);
	}
	public void requireRelive() {
		pst.ect.client.sender.reqRelive();
	}
	public void requreFire() {
		pst.ect.client.sender.reqFire(tank, (int)(Version.timespeed*Math.random()*1000));
	}
//	public void requirePosReset(){
//		pst.ect.client.sender.reqPosReset();
//	}
//	public void requireResetPos(){
//		final FlashInfo resetInfo = new FlashInfo();
//		resetInfo.cmd = NetConf.info_resetPos;
//		resetInfo.id = info.id;
//		pst.ect.client.sender.req(info, new CallBack<Info>() {
//			@Override
//			public void call(Info info) {
//				if(info.cmd==NetConf.info_resetPos){
//					tank.synchPos(new Pos(resetInfo.x,resetInfo.y));
//				}
//			}
//		});
//	}



}